const express = require('express');
const router = express.Router();
const pool = require('../../config/database');

router.get('/test', async (req, res) => {
  try {
    // 测试连接
    const connection = await pool.getConnection();
    connection.release();
    
    // 测试查询
    const [rows] = await pool.query('SELECT NOW() AS currentTime, VERSION() AS version');
    const sql = await pool.query('SELECT * FROM companies');

    
    
    res.json({
      status: 'success',
      message: '数据库连接正常',
      databaseInfo: sql[0]
    });
  } catch (err) {
    res.status(500).json({
      status: 'error',
      message: '数据库连接失败',
      error: err.message,
      details: {
        code: err.code,
        stack: process.env.NODE_ENV === 'development' ? err.stack : undefined
      }
    });
  }
});

module.exports = router;